#!/usr/bin/env python
# coding: utf-8

# In[1]:


print('hello word!')


# In[2]:


import time
time.sleep(3)


# In[3]:


import numpy as np
def square(x):
    return x * x


# In[4]:


x = np.random.randint(1, 10)
y = square(x)
print('%d squared is %d' % (x, y))


# In[5]:


def selection_sort(array):
    for i in range(len(array)-1):
        min_index = i
        for j in range(i+1, len(array)):
            if array[j] < array[min_index]:
                min_index = j
        if min_index != i:
            array[i], array[min_index] = array[min_index], array[i]
    return array


# In[6]:


array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
print(selection_sort(array))


# In[7]:


get_ipython().run_line_magic('matplotlib', 'inline')
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns


# In[8]:


df = pd.read_csv('fortune500.csv')


# In[9]:


df.head()


# In[10]:


df.tail()


# In[11]:


df.columns = ['year', 'rank', 'company', 'revenue', 'profit']


# In[12]:


len(df)


# In[13]:


df.dtypes


# In[14]:


non_numberic_profits = df.profit.str.contains('[^0-9.-]')
df.loc[non_numberic_profits].head()


# In[15]:


len(df.profit[non_numberic_profits])


# In[16]:


bin_sizes, _, _ = plt.hist(df.year[non_numberic_profits], bins=range(1955, 2006))


# In[17]:


df = df.loc[~non_numberic_profits]
df.profit = df.profit.apply(pd.to_numeric)


# In[18]:


len(df)


# In[19]:


df.dtypes


# In[20]:


group_by_year = df.loc[:, ['year', 'revenue', 'profit']].groupby('year')
avgs = group_by_year.mean()
x = avgs.index
y1 = avgs.profit
def plot(x, y, ax, title, y_label):
    ax.set_title(title)
    ax.set_ylabel(y_label)
    ax.plot(x, y)
    ax.margins(x=0, y=0)


# In[21]:


fig, ax = plt.subplots()
plot(x, y1, ax, 'Increase in mean Fortune 500 company profits from 1955 to 2005', 'Profit (millions)')


# In[22]:


y2 = avgs.revenue
fig, ax = plt.subplots()
plot(x, y2, ax, 'Increase in mean Fortune 500 company revenues from 1955 to 2005', 'Revenue (millions)')


# In[23]:


def plot_with_std(x, y, stds, ax, title, y_label):
    ax.fill_between(x, y - stds, y + stds, alpha=0.2)
    plot(x, y, ax, title, y_label)
fig, (ax1, ax2) = plt.subplots(ncols=2)
title = 'Increase in mean and std Fortune 500 company %s from 1955 to 2005'
stds1 = group_by_year.std().profit.values
stds2 = group_by_year.std().revenue.values
plot_with_std(x, y1.values, stds1, ax1, title % 'profits', 'Profit (millions)')
plot_with_std(x, y2.values, stds2, ax2, title % 'revenues', 'Revenue (millions)')
fig.set_size_inches(14, 4)
fig.tight_layout()


# In[ ]:




